import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/30 15:58
 * 说明：客户端发送文件给服务器端,服务器端将文件保存在本地，并返回“发送成功”给客户端，并关闭相应的链接
 */

public class TCPTest3 {
    @Test
    public void client() {
        Socket socket = null;
        FileInputStream fis = null;
        OutputStream os = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            // 1. socket
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            int port = 9090;
            socket = new Socket(inetAddress, port);

            // 2. file & fis
            File file = new File("好用的键盘.jpg");
            fis = new FileInputStream(file);
            // 3.Socket  -->  os
            os = socket.getOutputStream();

            // read & write
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1){
                os.write(buffer, 0, len);
            }
            System.out.println("complete");

            // 客户端表明不再继续发送数据
            socket.shutdownOutput();

            is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[5];
            int len1;
            while ((len1 = is.read(buffer1)) != -1){
                baos.write(buffer1, 0, len1);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 4. close()
            try {
                os.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                baos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Test
    public void server() {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            // 1. Socket
            int port = 9090;
            serverSocket = new ServerSocket(port);
            // 2. accept()
            socket = serverSocket.accept();
            System.out.println("Connect from: " + socket.getInetAddress().getHostAddress());

            // 3. 输入流
            is = socket.getInputStream();

            // 4. file & fos
            File file = new File("好用的键盘（服务器端）1.jpg");
            fos = new FileOutputStream(file);

            // 5. 读写
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                fos.write(buffer, 0, len);
            }
            System.out.println("complete");

            // 6. 服务端发送数据给客户端
            os = socket.getOutputStream();
            os.write("your pic is received".getBytes());

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 6. close()
            try {
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                os.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
